/*
 * @Author: your name
 * @Date: 2020-10-19 08:41:05
 * @LastEditTime: 2022-11-07 08:31:30
 * @LastEditors: zshawk1982
 * @Description: In User Settings Edit
 * @FilePath: \media_web_client\vue.config.js
 */
const IS_PROD = ["production", "prod"].includes(process.env.NODE_ENV);
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;

module.exports = {
  publicPath: './',
  // configureWebpack:{
  // module: {
  //   //elementplus配置
  //     rules: [
  //         {
  //             test: /\.mjs$/,
  //             include: /node_modules/,
  //             type: "javascript/auto"
  //         },
  //     ]
  // },
  // plugins:[
  //   new BundleAnalyzerPlugin()
  // ]
  // },
  configureWebpack: config => {
    //elementplus配置，解决elementplus和vuecli报错问题
    config.module.rules.push(
      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: "javascript/auto"
      },
    );
    //配置优化，完成chunk分割
    config.optimization = {
      splitChunks: {
        cacheGroups: {
          common: {
            name: "chunk-common",
            chunks: "initial",
            minChunks: 2,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 1,
            reuseExistingChunk: true,
            enforce: true
          },
          vendors: {
            name: "chunk-vendors",
            test: /[\\/]node_modules[\\/]/,
            chunks: "initial",
            priority: 2,
            reuseExistingChunk: true,
            enforce: true
          },
          'element-plus': {
            name: "chunk-element-plus",
            test: /[\\/]node_modules[\\/]element-plus[\\/]/,
            chunks: "all",
            priority: 3,
            reuseExistingChunk: true,
            enforce: true
          },
          echarts: {
            name: "chunk-echarts",
            test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
            chunks: "all",
            priority: 4,
            reuseExistingChunk: true,
            enforce: true
          }
        }
      }
    };
    const plugins = [];
    //生产阶段，进行gzip压缩处理
    if (IS_PROD) {
      plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path][base].gz",
          algorithm: "gzip",
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8
        })
      );
    }
    config.plugins = [...config.plugins, ...plugins];
  },
  chainWebpack: config => {
    // 打包分析
    config.plugin("webpack-report").use(BundleAnalyzerPlugin, [
      {
        analyzerMode: "static"
      }
    ]);
    // 忽略的打包文件,需要在index.html中补全cdn地址，虽可以极大降低包的大小，暂不使用，避免问题
    // config.externals({
    //   "@wangeditor/editor":"wangEditor",
    //   lodash: "_",
    //   "xlsx":"XLSX"
    // })
  }
  //代理
  // devServer: {
  //   proxy: {
  //     "/api": {
  //       target: "http://127.0.0.1:8081", // target host
  //       pathRewrite: {
  //         "^/api": "/", // rewrite path
  //       },
  //     },
  //   },
  // },
}